<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8" >
            <a-form-model-item label="院校代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeCode">
              <a-input v-model="model.collegeCode" placeholder="请输入院校代码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeName">
              <a-input v-model="model.collegeName" placeholder="请输入院校名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校英文" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeEnglishName">
              <a-input v-model="model.collegeEnglishName" placeholder="请输入院校英文" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeType">
              <j-dict-select-tag type="list" v-model="model.collegeType" dictCode="college_type" placeholder="请选择院校类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="办学类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="campusType">
              <j-dict-select-tag type="list" v-model="model.campusType" dictCode="campus_type" placeholder="请选择办学类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校层次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeLevel">
              <j-multi-select-tag type="list_multi" v-model="model.collegeLevel" dictCode="college_level" placeholder="请选择院校层次" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="办学性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeNature">
              <j-dict-select-tag type="list" v-model="model.collegeNature" dictCode="college_nature" placeholder="请选择办学性质" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="主管部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="competentAuth">
              <a-input v-model="model.competentAuth" placeholder="请输入主管部门" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="建校时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="foundingYear">
              <j-date placeholder="请选择建校时间" v-model="model.foundingYear" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="占地面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="acreage">
              <a-input v-model="model.acreage" placeholder="请输入占地面积" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校网址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="website">
              <a-input v-model="model.website" placeholder="请输入院校网址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="telephone">
              <a-input v-model="model.telephone" placeholder="请输入院校电话" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="电子邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" placeholder="请输入电子邮箱" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="博士点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="doctorDegree">
              <a-input v-model="model.doctorDegree" placeholder="请输入博士点" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="硕士点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="masterDegree">
              <a-input v-model="model.masterDegree" placeholder="请输入硕士点" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="地域标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="areaId">
		      <j-area-linkage type="cascader" v-model="model.areaId" placeholder="请输入省市区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="所在省份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="provinceId">
              <j-dict-select-tag type="list" v-model="model.provinceId" dictCode="" placeholder="请选择所在省份" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="所在城市" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityId">
              <j-dict-select-tag type="list" v-model="model.cityId" dictCode="" placeholder="请选择所在城市" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" placeholder="请输入地址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校简介" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="collegeProfiles">
              <a-input v-model="model.collegeProfiles" placeholder="请输入院校简介" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入说明" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="院校LOGO" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logo">
              <j-image-upload isMultiple  v-model="model.logo" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="招生计划" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="ceeEnrollPlanTable.loading"
          :columns="ceeEnrollPlanTable.columns"
          :dataSource="ceeEnrollPlanTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CollegeForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           collegeName: [
              { required: true, message: '请输入院校名称!'},
           ],
           collegeType: [
              { required: true, message: '请输入院校类型!'},
           ],
           campusType: [
              { required: true, message: '请输入办学类型!'},
           ],
           collegeLevel: [
              { required: true, message: '请输入院校层次!'},
           ],
           collegeNature: [
              { required: true, message: '请输入办学性质!'},
           ],
        },
        refKeys: ['ceeEnrollPlan', ],
        tableKeys:['ceeEnrollPlan', ],
        activeKey: 'ceeEnrollPlan',
        // 招生计划
        ceeEnrollPlanTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '地域',
              key: 'areaId',
              type: FormTypes.select,
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '年份',
              key: 'enrollYear',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '学科类别',
              key: 'subjectCategory',
              type: FormTypes.select,
              dictCode:"subject_category",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '招生批次',
              key: 'enrollBatch',
              type: FormTypes.select,
              dictCode:"enroll_batch",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '专业标识',
              key: 'specialtyId',
              type: FormTypes.select,
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '招生人数',
              key: 'enrollNum',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '学制年限',
              key: 'ageLimit',
              type: FormTypes.select,
              dictCode:"age_limit",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '说明',
              key: 'description',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/college/college/add",
          edit: "/college/college/edit",
          queryById: "/college/college/queryById",
          ceeEnrollPlan: {
            list: '/college/college/queryCeeEnrollPlanByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.ceeEnrollPlanTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.ceeEnrollPlan.list, params, this.ceeEnrollPlanTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          ceeEnrollPlanList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>